<template>
    <div class="com-container">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="请输入内容(限120字内)" maxlength="120" v-model="msg">
            


        </textarea>
        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
        <div class="cmt-list">

            <div class="cmt-item" v-for="(item,index) in comments" :key="index">
                <div class="cmt-title">
                    第{{ index+1 }}楼 &nbsp;&nbsp;用户：{{ item.usernames }}&nbsp;&nbsp;发表时间：{{ item.ctime | dateFormat('YYYY-MM-DD HH:mm:ss') }}

                   
                </div>
                 <div class="cmt-body">
                     {{ item.content }}
                        
                </div>
            </div>
        </div>

        <mt-button type="danger" size="large" plain @click="getmore()">加载更多</mt-button>
    </div>
    
</template>
<script>

import { Toast } from "mint-ui"
export default {
    data(){
        return{
            pageindex:1,
            comments:[],
            msg:'' ,//评论输入的内容
            user:{}

        }
    },
    created(){
        this.getcomments()

    },
    methods:{
        getcomments(){ //获取评论
            this.$http.get('https://www.apiopen.top/satinCommentApi?id=27610708&page=' + this.pageindex).then(result =>{
                console.log(result);
                if(result.status === 200){
                    // this.comments = result.body.data.normal.list;
                    //每当获取新评论数据的时候，不要把老数据清空而是以老数据拼接上新数据
                    this.comments = this.comments.concat(result.body.data.normal.list)

                }else{
                    Toast('获取失败')

                }

            })
        },
        getmore(){//加载更多
            this.pageindex++;
            this.getcomments()
            

        },
        postComment(){ //发表评论
           //看是否为空内容
           if(this.msg.trim().length === 0){
               return Toast('评论内容不能为空')
           }

           //参数1.请求的url地址
           //参数2.提交给服务器的评论内容
           //参数3.定义的数据格式{ emulateJSON=true }

            this.$http.post('https://www.apiopen.top/satinCommentApi?id=27610708&page=' + this.pageindex,{ content:this.msg.trim() }).then(result =>{
                console.log(result)
                if(result.status === 200){
                
                    var usernames = this.user.username
                    //1.拼接出一个评论对象
                    var cmt ={
                        usernames:'匿名用户',
                        ctime:Date.now(),
                        content:this.msg.trim()
                    };
                    this.comments.unshift(cmt);
                    this.msg = '';
                }else{
                    Toast('评论失败')
                }
            })


        }

        

    },
    props:["id"]
    
}
</script>
<style lang="scss" scoped>
.com-container{
   h3{
       font-size:  18px;
   }
   textarea{
       font-size: 14px;
       height: 85px;
       margin: 0px;
   }
   .cmt-list{
       margin: 5px 0;
       .cmt-item{
           font-size: 13px;
           .cmt-title{
               line-height: 30px;
               background-color: #ccc;

           }
           .cmt-body{
               line-height: 35px;
               text-indent: 2em;


           }

       }

   }

}

</style>



